****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings
****************************************************************************

clear all
set more off

use schlcode_avgchars_cms , clear
keep mathstand readstand schlcode
rename mathstand mathstand_hs 
rename readstand readstand_hs
rename schlcode homeschl

tempfile hsscores 
	save `hsscores'

use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

merge m:1 homeschl using `hsscores' , gen(mergehsscores)
keep if (mergehsscores==1 | mergehsscores==3)

gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)

xtset lottery 
	
lab var move1 "Mover (Change HS + Exit)"
lab var changehs "Change HS (Stay)"
lab var atthome1 "Attend Lagged HS"
lab var attappch1schl "Attend First Choice School"
lab var attassschl "Attend Assigned School"
lab var female "Female"
lab var lunch "Ec. Disadvantage"
lab var wonany "Won Any Choice"
lab var app2 "Made Second Choice"
lab var app3 "Made Third Choice"
lab var avgscores_hs "Avg. Math/RD HS Score"

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs if (wonfirst==1 & attassschl==1)
estadd scalar N_ind = e(N)
est store A

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs if (wonfirst==1 & attassschl==0)
estadd scalar N_ind = e(N)
est store B

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs if (wonfirst==0 & attassschl==1)
estadd scalar N_ind = e(N)
est store D

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs if (wonfirst==0 & attassschl==0)
estadd scalar N_ind = e(N)
est store E

xtreg mathstand_as move1
est store empty

** Kindergarten Summary (Columns 1, 2, 4, and 5 of Table A5)
esttab A B empty D E empty using summary-bycomply-kind.tex , replace ///
mtitles("Complier" "Non-Complier" " " "Complier" "Non-Complier" " ") ///
refcat(changehs "\textit{Outcomes}" female "\textit{Student Attributes}" , nolabel) ///
cells(mean(fmt(2)) sd(fmt(2)par)) label booktabs nonum collabels(none) gaps f ///
stats(N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}") labels(`"Observations"')) 


** Tests (Columns 3 and 6 of Table A5)
xtreg changehs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg changehs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , replace ///
mtitles("Won" " " "Lost") cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Change HS") ///
label booktabs nonum collabels(none) gaps f noobs plain

xtreg atthome1 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg atthome1 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Attend Lagged HS") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg attappch1schl attassschl if wonfirst==0
est store B 

esttab empty empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Attend First Choice School") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg wonany attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab empty empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Won Any") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg app2 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg app2 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Made Second Choice") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg app3 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg app3 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Made Third Choice") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg female attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg female attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Female") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg black attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg black attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Black") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg white attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg white attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "White") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg hispanic attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg hispanic attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Hispanic") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg avgscores_hs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg avgscores_hs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-kind.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Avg. Math/RD HS Score") ///
label booktabs nonum collabels(none) gaps f plain nomtitles


** Sixth Grade
use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & sixsample & stayer==1)

merge m:1 homeschl using `hsscores' , gen(mergehsscores)
keep if (mergehsscores==1 | mergehsscores==3)

gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)

xtset lottery 

lab var move1 "Mover (Change HS + Exit)"
lab var changehs "Change HS (Stay)"
lab var atthome1 "Attend Lagged HS"
lab var attappch1schl "Attend First Choice School"
lab var attassschl "Attend Assigned School"
lab var female "Female"
lab var lunch "Ec. Disadvantage"
lab var wonany "Won Any Choice"
lab var app2 "Made Second Choice"
lab var app3 "Made Third Choice"
lab var math_lag "Math Lag 1"
lab var read_lag "Read Lag 1"
lab var lep_Y_lag "LEP Lag 1"
lab var avgscores_hs "Avg. Math/RD HS Score"
 	
	
estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs math_lag read_lag lep_Y_lag if (wonfirst==1 & attassschl==1)
estadd scalar N_ind = e(N)
est store A

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs math_lag read_lag lep_Y_lag if (wonfirst==1 & attassschl==0)
estadd scalar N_ind = e(N)
est store B

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs math_lag read_lag lep_Y_lag if (wonfirst==0 & attassschl==1)
estadd scalar N_ind = e(N)
est store D

estpost sum changehs atthome1 attappch1schl wonany app2 app3 female black white hispanic lunch avgscores_hs math_lag read_lag lep_Y_lag if (wonfirst==0 & attassschl==0)
estadd scalar N_ind = e(N)
est store E
	
xtreg mathstand_as move1
est store empty

** Sixth Grade Summary (Columns 1, 2, 4, and 5 of Table A6)
esttab A B empty D E empty using summary-bycomply-six.tex , replace ///
mtitles("Complier" "Non-Complier" " " "Complier" "Non-Complier" " ") ///
refcat(changehs "\textit{Outcomes}" female "\textit{Student Attributes}" , nolabel) ///
cells(mean(fmt(2)) sd(fmt(2)par)) label booktabs nonum collabels(none) gaps f ///
stats(N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}") labels(`"Observations"')) 

** Tests (Columns 3 and 6 of Table A6)
xtreg changehs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg changehs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , replace ///
mtitles("Won" " " "Lost") cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Change HS (Stay)") ///
label booktabs nonum collabels(none) gaps f noobs plain

xtreg atthome1 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg atthome1 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Attend Lagged HS") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg attappch1schl attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg attappch1schl attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Attend First Choice School") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg wonany attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg wonany attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Won Any Choice") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg app2 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg app2 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Made Second Choice") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg app3 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg app3 attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Made Third Choice") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg female attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg female attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Female") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg black attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg black attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Black") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg white attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg white attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "White") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg hispanic attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg hispanic attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Hispanic") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles

xtreg avgscores_hs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg avgscores_hs attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Avg. Math/RD HS Score") ///
label booktabs nonum collabels(none) gaps f noobs plain nomtitles
	
xtreg math_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg math_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Math Lag 1") ///
label booktabs nonum collabels(none) gaps f	noobs plain nomtitles

xtreg read_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg read_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "Read Lag 1") ///
label booktabs nonum collabels(none) gaps f	noobs plain nomtitles

xtreg lep_Y_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==1 , fe cluster(lottery)
est store A 

xtreg lep_Y_lag attassschl lunch?? lunch_miss anyt1?? anyt1lunch?? esly?? if wonfirst==0 , fe cluster(lottery)
est store B 

esttab A empty B using tests-bycomply-six.tex , append ///
cells("b(fmt(3)star)" "se(fmt(3)par)") keep(attassschl) coef(attassschl "LEP Lag 1") ///
label booktabs nonum collabels(none) gaps f plain nomtitles
	

	
	
	
	
	
	
	
	
